using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Practices.EnterpriseLibrary.Security.Database;
using Microsoft.Practices.EnterpriseLibrary.Security.Configuration;

namespace CentralOffice.WebClient.Admin.Roles
{
    public partial class SystemFunctionEdit : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                SiteUtil.PopulateListBox(ddlFunctionType, SiteConstants.SystemFunction, false);
                if (!String.IsNullOrEmpty(Request.QueryString["ruleid"]))
                {
                    //AuthorizationProviderDatabaseProvider databaseProvider = new AuthorizationProviderDatabaseProvider();
                    //AuthorizationRuleDbData rule = databaseProvider.GetRule(new Guid(Request.QueryString["ruleid"]));

                    DbRuleManager dbRuleManager = SiteUtil.GetRuleManager();
                    DbAuthorizationRuleData rule = dbRuleManager.GetRule(new Guid(Request.QueryString["ruleid"]));

                    string exp = rule.Expression;
                    string expression = exp.Replace(" OR ", ",").Replace("R:", "");

                    txtFunction.Text = rule.Name;
                    txtRoles.Value = expression;
                    roles.Text = expression;

                    this.ViewState["RuleName"] = rule.Name;

                    txtFunction.Enabled = false;

                    ddlFunctionType.SelectedIndex = rule.Category;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            //IConfigurationSource configurationSource = ConfigurationSourceFactory.Create();
            //SecurityConfigurationView view = new SecurityConfigurationView(configurationSource);
            ////DbAuthorizationRuleProviderData ruleProviderData = view.GetAuthorizationProviderData(view.GetDefaultAuthorizationProviderName()) as DbAuthorizationRuleProviderData;
            //CustomAuthorizationProviderData ruleProviderData = view.GetAuthorizationProviderData(view.GetDefaultAuthorizationProviderName()) as CustomAuthorizationProviderData;

            //string database = null;
            //if (ruleProviderData != null)
            //{
            //    database = ruleProviderData.Attributes["database"];
            //    // database = ruleProviderData.Database;
            //}

            //AuthorizationProviderDatabaseProvider ruleProvider = new AuthorizationProviderDatabaseProvider(database);

            //AuthorizationProviderDatabaseProvider databaseProvider = new AuthorizationProviderDatabaseProvider();
            DbRuleManager dbRuleManager = SiteUtil.GetRuleManager();

            string expression = txtRoles.Value.TrimEnd(',');
            string[] rolesList = expression.Split(',');

            expression = "";
            for (int i = 0; i < rolesList.Length; i++)
            {
                if (i < rolesList.Length - 1)
                {
                    expression += "R:" + rolesList[i] + " OR ";
                }
                else
                {
                    expression += "R:" + rolesList[i];
                }
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ruleid"]))
            {
                dbRuleManager.UpdateById(new Guid(Request.QueryString["ruleid"]), (String)this.ViewState["RuleName"], expression, ddlFunctionType.SelectedIndex, txtFunction.Text);
            }
            else
            {
                dbRuleManager.Insert(Guid.NewGuid(), txtFunction.Text, expression, ddlFunctionType.SelectedIndex, txtFunction.Text);
            }

            Server.Transfer("SystemFunction.aspx");
        }
    }
}